"""
Case Type   : Timescaledb功能测试
Case Name   : drop_chunks异常场景验证,合理报错
Create At   : 2025/05/15
Owner       : lihongji
Description :
              1.drop_chunks使用空值
              2.drop_chunks使用数字、字符串
              3.drop_chunks使用now()函数
              4.清理环境
Expect      :
              1.合理报错
              2.合理报错
              3.合理报错
              4.清理成功
History     :
"""

import unittest
import os

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node


class Timescaledb(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.constant = Constant()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.Constant = Constant()
        self.primary_sh = CommonSH('PrimaryDbPGmode')
        self.db_user_node = Node(node='PrimaryDbPGmode')
        status = self.primary_sh.get_db_cluster_status()
        self.assertTrue("Normal" in status or "Degraded" in status)
        select_extension_cmd = f"SELECT * FROM pg_extension;"
        select_extension_result = self.primary_sh.execut_db_sql(
            select_extension_cmd)
        if "timescaledb" not in select_extension_result:
            create_extension_cmd = f"create extension timescaledb;"
            create_extension_result = (
                self.primary_sh.execut_db_sql(create_extension_cmd))
            self.assertIn(self.constant.extension_create_success,
                          create_extension_result, '创建timescaledb扩展失败')

    def test_add_dimension(self):
        text = '----step1:drop_chunks使用空值 expect:合理报错----'
        self.log.info(text)
        drop_chunks = """SELECT drop_chunks();
        SELECT drop_chunks(NULL::int);SELECT drop_chunks(NULL::interval);"""
        drop_chunks_result = self.primary_sh.execut_db_sql(drop_chunks)
        self.log.info(drop_chunks)
        self.log.info(drop_chunks_result)
        self.assertEqual(3, drop_chunks_result.count('ERROR'), '执行失败' + text)

        text = '----step2:drop_chunks使用数字、字符串 expect:合理报错----'
        self.log.info(text)
        drop_chunks = ("DROP TABLE IF EXISTS PUBLIC.drop_chunk_test3;"
                       "CREATE TABLE PUBLIC.drop_chunk_test3("
                       "time bigint, temp float8, device_id text);"
                       "SELECT create_hypertable('public.drop_chunk_test3', "
                       "'time', chunk_time_interval => 1, "
                       "create_default_indexes=>FALSE);"
                       "INSERT INTO PUBLIC.drop_chunk_test3 "
                       "VALUES(6, 6.0, 'dev7');"
                       "SELECT drop_chunks('haha', 'drop_chunk_test3');")
        drop_chunks_result = self.primary_sh.execut_db_sql(drop_chunks)
        self.log.info(drop_chunks)
        self.log.info(drop_chunks_result)
        self.assertEqual(1, drop_chunks_result.count('ERROR'),
                         '执行失败' + text)

        text = '----step3:drop_chunks使用now()函数 expect:合理报错----'
        self.log.info(text)
        drop_chunks = "SELECT drop_chunks(now(), 'drop_chunk_test3');"
        drop_chunks_result = self.primary_sh.execut_db_sql(drop_chunks)
        self.log.info(drop_chunks)
        self.log.info(drop_chunks_result)
        self.assertEqual(1, drop_chunks_result.count('ERROR'),
                         '执行失败' + text)

    def tearDown(self):
        text = '-----step4:清理环境 expect:成功-----'
        self.log.info(text)
        drop_table = "drop table PUBLIC.drop_chunk_test3"
        drop_table_result = self.primary_sh.execut_db_sql(drop_table)
        self.assertEqual(self.constant.DROP_TABLE_SUCCESS, drop_table_result,
                         '执行失败' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
